package cn.xd.itstack.springframework.context.support;

import cn.xd.itstack.springframework.beans.factory.config.BeanPostProcessor;
import cn.xd.itstack.springframework.context.ApplicationContext;
import cn.xd.itstack.springframework.context.ApplicationContextAware;

/**
 * @Classname ApplicationContextAwareBeanPostProcessor
 * @Description TODO
 * @Date 2022/5/23 下午2:50
 * @Created by lvxiaodong
 */
public class ApplicationContextAwareBeanPostProcessor implements BeanPostProcessor {

    private final ApplicationContext applicationContext;


    public ApplicationContextAwareBeanPostProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        if(bean instanceof ApplicationContextAware){
            ((ApplicationContextAware) bean).setApplicationContext(applicationContext);
        }
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        return bean;
    }
}
